package event

import (
	"encoding/json"
	"time"

	"github.com/sirupsen/logrus"

	"wespy-http-go/app/activity/2025/summer_drink_car/conf"
	"wespy-http-go/app/activity/2025/summer_drink_car/service"
	"wespy-http-go/app/activity/2025/summer_drink_car/store"
	"wespy-http-go/app/activity/common/actevent"
	"wespy-http-go/app/activity/common/actinfo"
	"wespy-http-go/app/activity/common/actlogger"
	"wespy-http-go/app/activity/common/actreward"
	"wespy-http-go/app/activity/common/actutil"
	"wespy-http-go/def"
)

// HandleCollectChip 处理收集碎片事件
func HandleCollectChip(data string) {
	entry := actlogger.GetLogger(actlogger.GenCtx(conf.ActId, 0))
	event := new(actevent.ActCollectChipEvent)
	err := json.Unmarshal([]byte(data), event)
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleCollectChip Unmarshal err")
		return
	}
	entry = entry.WithField("uid", event.Uid)
	if event.ActId != conf.ActId {
		return
	}
	actInfo := actinfo.GetValidActByUid(conf.ActId, event.Uid)
	if actInfo == nil {
		return
	}
	config := conf.GetActConfig(actInfo)
	if config == nil {
		return
	}
	entry.Infoln("summer_drink_car HandleCollectChip start")
	switch event.ChipId {
	case conf.ChipIdBuyFlag:
		handleBuyFlag(entry, config, event)
	}
	entry.Infoln("summer_drink_car HandleCollectChip end")
}

// handleBuyFlag 处理购买标志事件
func handleBuyFlag(entry *logrus.Entry, config *conf.ActConfig, event *actevent.ActCollectChipEvent) {
	err := service.OpenReceiveRewards(entry, config, event.Uid)
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car handleBuyFlag OpenReceiveRewards err")
		return
	}
	actutil.SendMessage(config.Messages.BuyBookMsg, actutil.SendMessageParams{
		SendUID: def.FaguanUid,
		RecvUID: event.Uid,
		ActID:   conf.ActId,
	})
	entry.Infoln("summer_drink_car handleBuyFlag end")
}

// HandleFreeFlyer 处理免费传单延迟任务
func HandleFreeFlyer(data string) {
	entry := actlogger.GetLogger(actlogger.GenCtx(conf.ActId, 0))
	event := new(store.FreeFlyerTask)
	err := json.Unmarshal([]byte(data), event)
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleFreeFlyer Unmarshal err")
		return
	}
	entry = entry.WithField("uid", event.Uid)
	actInfo := actinfo.GetValidActByUid(conf.ActId, event.Uid)
	if actInfo == nil {
		return
	}
	config := conf.GetActConfig(actInfo)
	if config == nil {
		return
	}
	entry.Infoln("summer_drink_car HandleFreeFlyer start")

	// 发放免费传单奖励
	chipReward := &actreward.RewardInfo{
		ActId:      conf.ActId,
		RewardType: actreward.RewardTypeCollectChip,
		RewardId:   conf.ChipIdFlyer,
		RewardVal:  config.FreeFlyerNumber,
	}
	err = actreward.SendRewards(event.Uid, []*actreward.RewardInfo{chipReward}, &actreward.ExtraInfo{
		ActId:      conf.ActId,
		ChipSource: "免费传单",
	})
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleFreeFlyer SendRewards err")
	}

	// 设置下次传单时间
	lastTime := time.Unix(int64(event.Timestamp), 0)
	err = service.NextFreeFlyer(config, event.Uid, actInfo.GetActCurrentTime(), lastTime)
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleFreeFlyer nextFreeFlyer err")
	}

	// 每日首次发送法官消息
	date := lastTime.Format(def.FormatTimeDayNoSeparator)
	cur, err := store.IncrFreeFlyerFlag(conf.ActId, event.Uid, date)
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleFreeFlyer IncrFreeFlyerFlag err")
	} else if cur == 1 {
		actutil.SendMessage(config.Messages.FreeFlyerMsg, actutil.SendMessageParams{
			SendUID: def.FaguanUid,
			RecvUID: event.Uid,
			ActID:   conf.ActId,
		})
	}

	entry.Infoln("summer_drink_car HandleFreeFlyer end")
}

// HandleCheckoutTable 顾客用餐时间结束后自动结算餐桌
func HandleCheckoutTable(data string) {
	entry := actlogger.GetLogger(actlogger.GenCtx(conf.ActId, 0))
	event := new(store.CheckoutTask)
	err := json.Unmarshal([]byte(data), event)
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleCheckoutTable Unmarshal err")
		return
	}
	entry = entry.WithField("uid", event.Uid)
	actInfo := actinfo.GetValidActByUid(conf.ActId, event.Uid)
	if actInfo == nil {
		return
	}
	config := conf.GetActConfig(actInfo)
	if config == nil {
		return
	}
	entry.Infoln("summer_drink_car HandleCheckoutTable start")
	num, err := service.CheckoutTablesSrv(entry, config, event.Uid, actInfo.GetActCurrentTime())
	if err != nil {
		entry.WithError(err).Errorln("summer_drink_car HandleCheckoutTable CheckoutTablesSrv err")
		return
	}
	entry.WithField("num", num).Infoln("summer_drink_car HandleCheckoutTable end")
}
